package com.qian.Test;

import java.awt.*;
import java.util.ArrayList;

import java.util.Scanner;

//工具类
public class Manag {
    /*
#### 3.工具类的规则：
**类名要见名知意如** ：arrUtil操作数组的工具类
**私有化构造方法**：因为工具类是不描述任何事物的
**方法定义为静态：**getMax() ,getSum(),getMin()
*/
    private Manag(){
    }

    //主页
    public static void homepage(){
        System.out.println("================欢迎使用学生管理系统=================");
        System.out.println("1:添加学生");
        System.out.println("2:删除学生");
        System.out.println("3:修改学生");
        System.out.println("4:查询学生");
        System.out.println("5:退出");
        System.out.println("请输入你的选择:");
    }


    //添加对象
    public static void addObject(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("你要添加的人数：");
        int n = sc.nextInt();
        if(!(n >= 1 && n <= 5)){
            System.out.println("==========输入错误！人数不能大于5，也不能是0人");
            return;
        }

        for (int i = 1; i <= n; i++) {
            //添加对象
            System.out.println("输入第"+i+"一个学生id:");
            int id = sc.nextInt();

            //判断id是否唯一
            boolean falg = verifyId(id,list);
            if(falg){
                System.out.println("=========id不唯一！");
                return;
            }
            System.out.println("输入学生姓名:");
            String name = sc.next();
            System.out.println("输入学生年龄:");
            int age = sc.nextInt();
            System.out.println("输入学生家庭地址:");
            String site = sc.next();
            //添加对象
            Student student = new Student(id,name,age,site);
            list.add(student);

            System.out.println("==========添加成功！");

            System.out.println("现有人数："+list.size()+"人");

        }
    }

    //判断id是否同唯一
    public static Boolean verifyId(int setid, ArrayList<Student> list){
        //遍历集合
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId() == setid){
                //id唯一
                return true;
            }
        }

        //id不唯一
        return false;
    }
    //删除对象
    public static void deleteObject(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);

        System.out.println("输入你要删除学生的id：");
        int id = sc.nextInt();
        boolean falg = verifyId(id,list);
        if(!falg){
            System.out.println("=========没有找到id!");
            return;
        }

        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId() == id){
                list.remove(i); //删除整个对象
                System.out.println("删除完成");
            }
        }
    }

    //查看对象
    public static void lookObject(ArrayList<Student> list){
        //遍历容器里面的对象并打印内容
        for (int i = 0; i < list.size(); i++) {
            list.get(i).display();
        }
    }

    //修改对象
    public static void revampObject(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);

        System.out.println("输入你要修改的学生id");
        int id = sc.nextInt();
        boolean falg = verifyId(id,list);
        if(!falg){
            System.out.println("id不存在");
        }

        for (int i = 0; i < list.size(); i++) {
             //判断学生找到指定id的对象
            if(list.get(i).getId() == id){
                //找到id进行修改
                System.out.println("输入新id");
                int newid = sc.nextInt();
                list.get(i).setId(newid);

                System.out.println("输入新名字");
                String newname = sc.next();
                list.get(i).setName(newname);

                System.out.println("输入新年龄：");
                int newage = sc.nextInt();
                list.get(i).setAge(newage);

                System.out.println("请输入新地址：");
                String newsite = sc.next();
                list.get(i).setSite(newsite);

            }
        }
    }
}
